package com.springboot.system.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.springboot.common.utils.StringUtils;
import com.springboot.core.jdbc.BaseDao;
import com.springboot.core.web.mvc.Page;
import com.springboot.system.domain.log.SysLog;
import com.springboot.system.domain.log.SysLogVO;

/**
 * 业务日志数据持久层实现类
 * 
 * @ClassName: LogDao
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 252956
 * @date 2021年1月7日 上午9:42:45
 *
 */
@Repository
public class LogDao extends BaseDao {

	public Page<SysLog> selectPage(SysLogVO log) {

		List<SysLog> list = super.select(
				getSqlPageHandle().handlerPagingSQL(logPageSql(log, 0), log.getPage(), log.getLimit()), null,
				SysLog.class);
		int count = super.getJdbcTemplate().queryForObject(logPageSql(log, 1), null, Integer.class);
		Page<SysLog> page = new Page<SysLog>(list, count, log.getLimit(), log.getPage());
		return page;
	}

	private String logPageSql(SysLogVO log, int type) {
		StringBuilder sql = new StringBuilder();
		if (type == 0) {
			sql.append(
					"select  id,creator,title,url,request_method as requestmethod,content_type as contentType,request_params as requestparams,ip,create_time as createTime,duration,response_result as result,status from t_sys_log");
		} else {
			sql.append("select count(*) from t_sys_log ");
		}
		sql.append(" where 1=1");

		sql.append(" and DATE_FORMAT(create_time,'%Y-%m-%d') >='" + log.getStartTime() + "'");

		sql.append(" and DATE_FORMAT(create_time,'%Y-%m-%d') <='" + log.getEndTime() + "'");

		if (StringUtils.isNotBlank(log.getCreator())) {
			sql.append(" and creator like '%").append(log.getCreator().trim() + "%' ");
		}
		if (StringUtils.isNotBlank(log.getTitle())) {
			sql.append(" and title like '%").append(log.getTitle().trim() + "%' ");
		}
		if (log.getStatus() != null) {
			sql.append(" and status=" + log.getStatus() + "");
		}

		if (type == 0) {
			sql.append(" order by create_time desc");
		}
		return sql.toString();
	}

}
